part of '../repository.dart';

class _RevokedPurchasedItemDto extends Equatable {
  const _RevokedPurchasedItemDto._internal({
    required this.orderId,
    required this.purchaseToken,
    required this.revocationTimeMs,
    required this.revocationSourceCode,
    required this.revocationReasonCode,
  });

  final String orderId;
  final String purchaseToken;
  final int revocationTimeMs;
  final int revocationSourceCode;
  final int revocationReasonCode;

  @override
  List<Object> get props {
    return [
      orderId,
      purchaseToken,
      revocationTimeMs,
      revocationSourceCode,
      revocationReasonCode,
    ];
  }

  // factory _RevokedPurchasedItemDto.fromJson(Map<String, dynamic> json) {
  //   return _RevokedPurchasedItemDto._internal(
  //     orderId: json['orderId'],
  //     purchaseToken: json['purchaseToken'],
  //     revocationTimeMs: int.parse(json['voidedTimeMillis']),
  //     revocationSourceCode: json['voidedSource'],
  //     revocationReasonCode: json['voidedReason'],
  //   );
  // }

  factory _RevokedPurchasedItemDto.fromApiVoidedPurchase(VoidedPurchase apiVoidedPurchase) {
    return _RevokedPurchasedItemDto._internal(
      orderId: apiVoidedPurchase.orderId!,
      purchaseToken: apiVoidedPurchase.purchaseToken!,
      revocationTimeMs: int.parse(apiVoidedPurchase.voidedTimeMillis!),
      revocationSourceCode: apiVoidedPurchase.voidedSource!,
      revocationReasonCode: apiVoidedPurchase.voidedReason!,
    );
  }

  GooglePlayApiRevokedPurchasedItem toEntity() {
    late final GooglePlayApiTransactionRevocationSource revocationSource;

    if (revocationSourceCode == 0) {
      revocationSource = GooglePlayApiTransactionRevocationSource.user;
    } else if (revocationSourceCode == 1) {
      revocationSource = GooglePlayApiTransactionRevocationSource.developer;
    } else if (revocationSourceCode == 2) {
      revocationSource = GooglePlayApiTransactionRevocationSource.google;
    }

    late final GooglePlayApiTransactionRevocationReason revocationReason;

    if (revocationReasonCode == 0) {
      revocationReason = GooglePlayApiTransactionRevocationReason.other;
    } else if (revocationReasonCode == 1) {
      revocationReason = GooglePlayApiTransactionRevocationReason.remorse;
    } else if (revocationReasonCode == 2) {
      revocationReason = GooglePlayApiTransactionRevocationReason.notReceivedPurchase;
    } else if (revocationReasonCode == 3) {
      revocationReason = GooglePlayApiTransactionRevocationReason.defectivePurchase;
    } else if (revocationReasonCode == 4) {
      revocationReason = GooglePlayApiTransactionRevocationReason.accidentalPurchase;
    } else if (revocationReasonCode == 5) {
      revocationReason = GooglePlayApiTransactionRevocationReason.fraud;
    } else if (revocationReasonCode == 6) {
      revocationReason = GooglePlayApiTransactionRevocationReason.friendlyFraud;
    } else if (revocationReasonCode == 7) {
      revocationReason = GooglePlayApiTransactionRevocationReason.chargeback;
    }

    final revocationTime = DateTime.fromMillisecondsSinceEpoch(
      revocationTimeMs,
      isUtc: true,
    );

    return GooglePlayApiRevokedPurchasedItem(
      purchaseToken: purchaseToken,
      orderId: orderId,
      revocationSource: revocationSource,
      revocationReason: revocationReason,
      revocationTime: revocationTime,
    );
  }
}
